package ch.exm.storm.session;

import ch.exm.storm.query.QueryFactory;
import ch.exm.storm.query.hibernate.HibernateQueryFactory;
import ch.exm.storm.query.inmemory.InMemoryQueryFactory;

public class SessionFactory {

	public static final String IN_MEMORY_ENGINE = "inMemory";
	public static final String HIBERNATE_ENGINE = "hibernate";
	
	private static final String DEFAULT_ENGINE = IN_MEMORY_ENGINE;
	
	protected SessionFactory() { }
	
	private QueryFactory queryFactory;
	public QueryFactory getQueryFactory() {
		if (queryFactory == null) {
			if (engineType.equalsIgnoreCase(IN_MEMORY_ENGINE)) queryFactory = new InMemoryQueryFactory();
			else if (engineType.equalsIgnoreCase(HIBERNATE_ENGINE)) queryFactory = new HibernateQueryFactory();
			else throw new RuntimeException("Unknown engine type: " + engineType);
		}
		return queryFactory;
	}
	
	private String engineType = DEFAULT_ENGINE;
	public void setEngineType(String engineType) {
		this.engineType = engineType;
	}
	
	public StormSession openSession() {
		return new StormSession(getQueryFactory());
	}
}
